<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
    <title>投票管理</title>
	<%@ include file="/common/header/meta.jsp"%>
	<%@ include file="/common/header/script.jsp"%>
    <script type="text/javascript">
    var $tg ;
	$(function() {
		$tg = $("#tg").treegrid({
			url : $.webapp.root+"/admin/system/vote/tree.do", border: false,
			title: '<span style="font-weight:normal;color:#666;">投票管理</span>',
			idField: 'id', treeField: 'subject', rownumbers: true, cascadeCheck: true, lines: true,
			toolbar: '#toolbars', striped: true, fit: true, remoteSort: false, singleSelect: true,
			frozenColumns: [[
				{ field: 'ck', checkbox: true },
				{ field: 'id', title: 'ID', hidden: true }
			]],
			columns: [[
	    		{ field: 'weight', title: '排序', width: 50, sortable: true, align: 'center', editor: "numberbox" },
			    { field: 'isTitle', title: '类型', width: 150, align: 'center', hidden: true, formatter:function(value,row){
			    	if(value == true){return "<font color='red'>主标题</font>";}else {return "<font color='#5CB811'>子标题</font>";}
			    }},
	            { field: 'subject', title: '标题', width: 320, sortable: true },
			    { field: 'sum', title: '票数', width: 100, align: 'center', formatter:function(value,row){
			    	if(row.isTitle == false) {
			    		return value ;
			    	}
			    }},
			    { field: 'percent', title: '百分比%', width: 100, align: 'center', formatter:function(value,row){
			    	if(row.isTitle == false) {
			    		if(row.totalVote > 0) {
				    		return value ;
			    		} else {
			    			return "0%";
			    		}
			    	}
			    }},
			    { field: 'opers', title: '操作', width: 140, align: 'center', formatter:function(value,row){
			    	if(row.isTitle) {
			    		var str = $.string.format("<a href='#' onclick='javascript:addSubVote(\"{0}\")'>添加子项</a>", row.id) ; 
			    		str+= $.string.format("&nbsp;&nbsp;<a href='#' onclick='javascript:changeStatus(\"{0}\")'>{1}</a>", row.id, (row.status==true?"<span style='color:red;'>开启投票</span>":"关闭投票")) ;
			    		return str;
			    	} else {
			    		var str = $.string.format("<a href='#' onclick='javascript:updateSubVote(\"{0}\")'>编辑</a>", row.id) ;
			    		return str ;
			    	}
			    	
			    }},
			    { field: 'created', title: '创建时间', width: 140, sortable: true }
			]],
			autoEditing: true, extEditing: true, singleEditing: true,
            onBeforeEdit: function(data) {
        		$.fn.treegrid.extensions.onBeforeEdit.apply(this, arguments);
	        	pid = data.pid ; oldSort = data.weight ;
	        },
	        onAfterEdit: function(data) {
	            $.fn.treegrid.extensions.onAfterEdit.apply(this, arguments);
	            if(oldSort != data.weight) {
	            	var o = {oldSort: oldSort, newSort: data.weight, id: data.id, pid: (undefined == data.pid?"":data.pid)};
	            	$.post($.webapp.root + "/admin/system/vote/doNotNeedAuth_sort.do", o, function(result) {
	            		$tg.treegrid("reload") ; $.easyui.loaded();
	        		}).error(function() { $.easyui.loaded(); });
	            }
	        },
		});
	});
	
	function form_edit(form) {
		var form_url = $.webapp.root + "/admin/system/vote/vote_form_UI.do" ;
		if("E" == form) {
			var node = $tg.treegrid('getSelected');
			if (node) {
				if(undefined != node.pid) {
					alertify.warning("不可编辑投票子项！"); return ;
				}
				form_url = $.webapp.root + "/admin/system/vote/vote_form_UI.do?id="+node.id ;
			} else {
				alertify.warning("请选择一条记录！"); return ;
			}
		}
		var $d = $.easyui.showDialog({
			href: form_url, title: "投票表单", iniframe: false, topMost: true, width: 440 , height: 225,
	        enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
	        buttons : [ 
			  { text : '保存并继续', iconCls : 'icon-standard-disk', handler : function() { $.easyui.parent.submitForm($d, $tg, true) ; } },
	          { text : '确定', iconCls : 'ext_save', handler : function() { $.easyui.parent.submitForm($d, $tg) ; } },
	          { text : '关闭', iconCls : 'ext_cancel', handler : function() { $d.dialog('destroy'); } }
	       	]
	    });
	}
	function addSubVote(id) {
		var form_url = $.webapp.root + "/admin/system/vote/vote_add_sub_UI.do?id="+id ;
		var $d = $.easyui.showDialog({
			href: form_url, title: "投票表单", iniframe: false, topMost: true, width: 480 , height: 205,
	        enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
	        buttons : [ 
			  { text : '保存并继续', iconCls : 'icon-standard-disk', handler : function() { $.easyui.parent.submitForm($d, $tg, true) ; } },
	          { text : '确定', iconCls : 'ext_save', handler : function() { $.easyui.parent.submitForm($d, $tg) ; } },
	          { text : '关闭', iconCls : 'ext_cancel', handler : function() { $d.dialog('destroy'); } }
	       	]
	    });
	}
	function updateSubVote(id) {
		var form_url = $.webapp.root + "/admin/system/vote/vote_update_sub_UI.do?id="+id ;
		var $d = $.easyui.showDialog({
			href: form_url, title: "投票表单", iniframe: false, topMost: true, width: 480 , height: 205,
	        enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
	        buttons : [ 
	          { text : '确定', iconCls : 'ext_save', handler : function() { $.easyui.parent.submitForm($d, $tg) ; } },
	          { text : '关闭', iconCls : 'ext_cancel', handler : function() { $d.dialog('destroy'); } }
	       	]
	    });
	}
	
	function del() {
		var node = $tg.treegrid('getSelected');
		if(node){
			$.messager.confirm("该操作不可逆，您确定要进行该操作？", function (c) { 
				if(c) {
					$.easyui.loading({ msg: "数据删除中，请稍等..." });
					$.post($.webapp.root + "/admin/system/vote/delete.do", {id:node.id}, function(result) {
						if (result.status) {
							$tg.treegrid('reload') ; $.easyui.loaded();
							alertify.success(result.msg);
						} else {
							$.easyui.loaded();alertify.error(result.msg);
						}
					}, 'json').error(function(){$.easyui.loaded();});
				} else {$.easyui.loaded("#westCenterLayout", true);}
			});
		} else {
			alertify.warning("请选择一条记录！");
		}
	}
	function changeStatus(id) {
		$.easyui.loading({ msg: "数据删除中，请稍等..." });
		$.post($.webapp.root + "/admin/system/vote/doNotNeedAuth_closeVote.do", {id:id}, function(result) {
			$tg.treegrid('reload') ; $.easyui.loaded();
		}, 'json').error(function(){$.easyui.loaded();});
	}
	
	
	function mail() {
		var node = $tg.treegrid('getSelected');
		if (node) {
			if(undefined != node.pid) {
				alertify.warning("请选择投票主题！"); return ;
			}
		} else {
			alertify.warning("请选择一条记录！"); return ;
		}
		
		var $d = $.easyui.showDialog({
			href: $.webapp.root+"/admin/system/vote/vote_mail_UI.do?id="+node.id, title: "邮件通知", iniframe: false, topMost: true,
			width: 750, height: 465, maximizable: true,
            enableApplyButton: false, enableCloseButton: false,  enableSaveButton: false,
            buttons : [ 
              { text : '发送', iconCls : 'ext_save', handler : function() { $.easyui.parent.submitForm($d, $tg); } },
              { text : '关闭', iconCls : 'ext_cancel', handler : function() { $d.dialog('destroy'); } } 
           	]
        });
	}
    </script>
</head>

<body>

	<div id="tg">
		<div id="toolbars">
            <a onClick="form_edit('A');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_add'">添加</a>
            <a onClick="form_edit('E');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_edit'">编辑</a>
            <a onClick="del();" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_remove'">删除</a>
            <a onclick="$tg.treegrid('reload');" class="easyui-linkbutton" data-options="plain: true, iconCls: 'ext_reload'">刷新</a>
            
            <a onclick="mail();" class="easyui-linkbutton" data-options="plain: true, iconCls:'ext_email'">邮件通知</a>
        </div>
	</div>
    
</body>
</html>



